package com.maxd.controller.commodity;

import com.alibaba.fastjson.JSONArray;
import com.maxd.adapter.IgnoreSecurity;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.*;
import com.maxd.service.Commodity.CommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: 房
 * \* Date: 2020/5/20
 * \* Time: 13:49
 * \*
 * \
 */
@RestController
@Api(value = "商品", tags = {"商品"})
@RequestMapping(value = "/commodity")
public class CommodityController {

    @Autowired
   private CommodityService commodityService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation("添加商品")
    @ResponseBody
    public Result save(@RequestBody Commodity commodity) {
        Commodity save = commodityService.save(commodity);
        if(save==null){
            return ResultUtil.error(-200,"该商品已经添加过了！");
        }
        return ResultUtil.success();
    }

    @RequestMapping(value = "/saveList", method = RequestMethod.POST)
    @ApiOperation("添加商品多个商品")
    @ResponseBody
    public Result saveList(String commoditys) {
        List<Commodity> list=JSONArray.parseArray(commoditys,Commodity.class);
        commodityService.saveAll(list);
        return ResultUtil.success();
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation("修改商品")
    @ResponseBody
    public Result update(@RequestBody Commodity commodity) {
        commodityService.update(commodity);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ApiOperation("删除商品")
    @ResponseBody
    public Result deleteById(Long id) {
        commodityService.deleteById(id);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/deleteByIdList", method = RequestMethod.POST)
    @ApiOperation("删除多个商品")
    @ResponseBody
    public Result deleteByIdList(String ids) {
        commodityService.deleteByIdList(ids);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    @ApiOperation("根据id查看详细信息")
    @ResponseBody
    public Result selectById(Long id) {
        return ResultUtil.success(commodityService.selectById(id));
    }

    @RequestMapping(value = "/selectByUserId", method = RequestMethod.GET)
    @ApiOperation("商品列表根据用户id查看")
    @ResponseBody
    public Result selectByUserId(Long userId,int page,int size) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return ResultUtil.success(commodityService.selectByUserId(userId,pageable));
    }

    @RequestMapping(value = "/selectCommodityList", method = RequestMethod.GET)
    @ApiOperation("查看所有信息")
    @IgnoreSecurity
    @ResponseBody
    public Result selectCommodityList(int page,int size) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return ResultUtil.success(commodityService.selectCommodityList(pageable));
    }



}